<template>
  <div class="Category">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top:15px;">
      <el-table :data="CateList" style="width: 100%">
        <el-table-column label="排序" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="name"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分类编辑 -->
    <el-dialog title="分类修改" :visible.sync="CateDialogVisible" width="50%">
      <el-form :model="editForm" status-icon :rules="EditRules" ref="EditRef" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="CateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'Category',
  data() {
    return {
      CateList: [],
      CateDialogVisible: false,
      editForm: {
        name: '',
        id: ''
      },
      EditRules: {
        name: [{ required: true, message: '分类字段不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getCate()
  },
  methods: {
    getCate() {
      this.$axios.get('/api/admin/category').then(result => {
        // console.log(result)
        this.CateList = result.data
      })
    },
    handleEdit(index, row) {
      console.log(row)
      ;(this.CateDialogVisible = true), (this.editForm.name = row.name)
      this.editForm.id = row._id
    },
    handleDelete() {},
    submitEdit() {
      this.$axios
        .put(`/api/admin/category/${this.editForm.id}`, {
          name: this.editForm.name
        })
        .then(result => {
          this.$message({
            type: result.data.type,
            message: result.data.msg
          })
          this.CateDialogVisible = false
          this.getCate()
        })
    }
  }
}
</script>